package com.yuwei.home.parking_space_management.add_car_view



import android.view.ContextThemeWrapper
import android.view.View
import android.widget.PopupMenu
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.yuwei.base.BaseActivity
import com.yuwei.common.path.RouterPath
import com.yuwei.home.AddParkingItemEntity
import com.yuwei.home.R
import com.yuwei.home.databinding.ActivityAddParkingBinding
import com.yuwei.home.parking_space_management.car_adapter.AddParkingZTAdapter
import com.yuwei.http.entity.AddParkingEntity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.ADD_PARKING_SPACE_MANAGEMENT)
class Add_ParkingActivity : BaseActivity<ActivityAddParkingBinding,AddParkingViewModel>() {
    val addParkingZTAdapter = AddParkingZTAdapter()
    var lastSelectedPosition = -1
    var textZt:String = ""
    override fun initView() {
        binding.addParkingZtRv.adapter = addParkingZTAdapter
        binding.addParkingZtRv.layoutManager = GridLayoutManager(this,3)
        addParkingZTAdapter.setOnItemClickListener { adapter, view, position ->
            if (lastSelectedPosition != position) {
                // 取消上一次选中的项
                if (lastSelectedPosition != -1) {
                    addParkingZTAdapter.data[lastSelectedPosition].isShow = false
                    addParkingZTAdapter.notifyDataSetChanged()
                }
                // 选中当前项
                addParkingZTAdapter.data[position].isShow = true
                textZt = addParkingZTAdapter.data[position].title
                if (textZt == "已占用"){
                    binding.parkingTt41.visibility = View.VISIBLE
                    binding.parkingTt4.visibility =  View.VISIBLE
                    binding.parkingTt51.visibility =  View.VISIBLE
                    binding.parkingTt5.visibility =  View.VISIBLE
                    binding.parkingTt51.visibility =  View.VISIBLE
                    binding.parkingTt5.visibility =  View.VISIBLE
                    binding.parkingTt61.visibility = View.VISIBLE
                    binding.parkingTt6.visibility =  View.VISIBLE
                }else{
                    binding.parkingTt41.visibility = View.GONE
                    binding.parkingTt4.visibility =  View.GONE
                    binding.parkingTt51.visibility =  View.GONE
                    binding.parkingTt5.visibility =  View.GONE
                    binding.parkingTt51.visibility =  View.GONE
                    binding.parkingTt5.visibility =  View.GONE
                    binding.parkingTt61.visibility = View.GONE
                    binding.parkingTt6.visibility =  View.GONE
                }
                lastSelectedPosition = position
                addParkingZTAdapter.notifyDataSetChanged()
            }
        }
        binding.parkingTt21.setOnClickListener {
            val popupMenu = PopupMenu(this,it)
            popupMenu.menuInflater.inflate(R.menu.add_parking_menu,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener {item ->
                when(item.itemId){
                    R.id.tt1 ->{
                        binding.parkingTt2.setText(item.title)
                        true
                    }
                    R.id.tt2 ->{
                        binding.parkingTt2.setText(item.title)
                        true
                    }
                    R.id.tt3 ->{
                        binding.parkingTt2.setText(item.title)
                        true
                    }
                    R.id.tt4 ->{
                        binding.parkingTt2.setText(item.title)
                        true
                    }
                    R.id.tt5 ->{
                        binding.parkingTt2.setText(item.title)
                        true
                    }
                    R.id.tt6 ->{
                        binding.parkingTt2.setText(item.title)
                        true
                    }
                    else -> super.onOptionsItemSelected(item)
                }
            }
            popupMenu.show()
        }


        binding.parkingTt32.setOnClickListener {
            val popupMenu = PopupMenu(this,it)
            popupMenu.menuInflater.inflate(R.menu.add_parking_cw_menu,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener {item ->
                when(item.itemId){
                    R.id.tt1 ->{
                        binding.parkingTt3.setText(item.title)
                        true
                    }
                    R.id.tt2 ->{
                        binding.parkingTt3.setText(item.title)
                        true
                    }
                    R.id.tt3 ->{
                        binding.parkingTt3.setText(item.title)
                        true
                    }
                    R.id.tt4 ->{
                        binding.parkingTt3.setText(item.title)
                        true
                    }

                    else -> super.onOptionsItemSelected(item)
                }
            }
            popupMenu.show()
        }
        binding.parkingButton.setOnClickListener {
            val addParkingEntity = AddParkingEntity(
                areaId = 2,
                areaName = binding.parkingTt2.text.toString(),
                id = 0,
                licensePlate = binding.parkingTt5.text.toString(),
                remarks = "",
                spaceNumber = binding.parkingTt1.text.toString(),
                spaceStatus = textZt.toString(),
                spaceType = binding.parkingTt3.text.toString(),
                userId = 11,
                userName = binding.parkingTt4.text.toString()
            )
            vm.getAddParking(addParkingEntity)
        }
        binding.addParkingBack.setOnClickListener {
            finish()
        }
        binding.parkingAdd.setOnClickListener {
            finish()
        }

    }


    override fun initData() {
        addParkingZTAdapter.data.clear()
        addParkingZTAdapter.data.add(AddParkingItemEntity("空闲",false))
        addParkingZTAdapter.data.add(AddParkingItemEntity("已占用",false))
        addParkingZTAdapter.data.add(AddParkingItemEntity("维护中",false))
        addParkingZTAdapter.notifyDataSetChanged()
        vm.getAddParkingSuccess.observe(this){
            ToastUtils.showLong("保存成功")
            finish()
        }

    }



}